using System;
using System.Collections;
using ThunderRoad;
using UnityEngine;
using Random = UnityEngine.Random;

namespace BBYScripts
{
    public class ChargeCount : MonoBehaviour
    {
        public int maxCharge = 10;
        public float radiosRate = 0.03f;
        public ItemSpawner spawner;
        public AudioSource chargeSound;
        public AudioSource shootSound;
        public ParticleSystem chargeParticles;
        public ParticleSystem fireParticles;
        public float stepMinSec = 0.2f;
        public float stepMaxSec = 0.5f;
        private int _chargeStartTime = 0;
        private bool _stepShooting = false;
        private bool _usable = true;

        public void Usable()
        {
            _usable = true;
        }

        public void UnUsable()
        {
            _usable = false;
        }

        public void ChargeStart()
        {
            if (_stepShooting || !_usable)
            {
                return;
            }

            if (chargeSound)
            {
                _chargeStartTime = DateTime.Now.Second;
                chargeSound.Play();
            }

            if (chargeParticles)
            {
                chargeParticles.Play();
            }

            StartCoroutine(WaitForAudioToFinish());
        }

        private IEnumerator WaitForAudioToFinish()
        {
            // 等待音频播放完毕
            while (_usable && chargeSound.isPlaying)
            {
                yield return null;
            }

            // 音频播放完毕后调用函数
            Billing(false);
        }

        public void Billing()
        {
            Billing(true);
        }

        private void Billing(bool shoot)
        {
            if (_chargeStartTime == 0)
            {
                return;
            }

            if (chargeSound)
            {
                chargeSound.Stop();
            }

            if (chargeParticles)
            {
                chargeParticles.Stop();
            }

            var count = GetCount();
            if (_usable && shoot && spawner)
            {
                if (count > 5)
                {
                    StartCoroutine(StepShoot(count));
                }
                else
                {
                    DoSpawn(count);
                }
            }

            _chargeStartTime = 0;
        }

        public void DoSpawn(int count)
        {
            spawner.spawnCount = count;
            spawner.randomRadius = count * radiosRate;
            if (fireParticles)
            {
                fireParticles.Play();
            }

            spawner.Spawn();
            if (shootSound)
            {
                shootSound.Play();
            }
        }

        // 协程，用于分三次调用Spawn方法
        private IEnumerator StepShoot(int count)
        {
            // 确定每次生成的数量
            var firstSpawnCount = Math.Max(count / 10, 1);
            var secondSpawnCount = Math.Max(3 * count / 10, 1);
            var thirdSpawnCount = count - firstSpawnCount - secondSpawnCount; // 第三次生成剩余的物品

            // 第一次生成
            DoSpawn(firstSpawnCount);
            yield return new WaitForSeconds(Random.Range(stepMinSec, stepMaxSec)); // 随机等待

            // 第二次生成
            DoSpawn(secondSpawnCount);
            yield return new WaitForSeconds(Random.Range(stepMinSec, stepMaxSec)); // 再次随机等待

            // 第三次生成
            DoSpawn(thirdSpawnCount);
            _stepShooting = false;
        }

        private int GetCount()
        {
            if (_chargeStartTime == 0)
            {
                return 1;
            }

            var count = DateTime.Now.Second - _chargeStartTime;
            count = count * maxCharge / 6; // 只有6s
            if (count < 1) return 1;
            return count > maxCharge ? maxCharge : count;
        }
    }
}